{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Examples require an initialized GsSession and relevant entitlements. External clients need to substitute thier own client id and client secret below. Please refer to [Authentication](https://developer.gs.com/p/docs/institutional/platform/authentication/) for details."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant.session import GsSession\n",
    "GsSession.use(client_id=None, client_secret=None, scopes=('read_product_data',))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from gs_quant.api.gs.assets import GsAssetApi\n",
    "from gs_quant.data import Dataset\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "ds = Dataset('EDRVOL_PERCENT_STANDARD')  # https://marquee.gs.com/s/developer/datasets/EDRVOL_PERCENT_STANDARD\n",
    "ids = ds.get_coverage().get('assetId')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Filtering coverage by Asset properties\n",
    "A lot of datasets use Marquee Asset Id as symbol dimension. Marquee Asset Service to get more properties for these assets and/or filter by them.\n",
    "Below example showcases how to filter assets by Region"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "# find all assets from given region (One of \"Asia\", \"Europe\", \"Americas\")\n",
    "def get_assets_in_region(region, asset_ids):\n",
    "    step = 500\n",
    "    size = asset_ids.shape[0]\n",
    "    assets = []\n",
    "    # go over all assets in the dataset in batches of 500 and query for region\n",
    "    for i in range(0, size, step):\n",
    "        end = min(i + step, size)\n",
    "        # prepare the query\n",
    "        query = {'id': asset_ids.values[i: end].tolist(), 'region': region}\n",
    "        # run the query\n",
    "        results = GsAssetApi.get_many_assets_data(limit=step, **query)\n",
    "        assets.extend(results)\n",
    "    return assets"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "pycharm": {
     "name": "#%%\n"
    }
   },
   "outputs": [],
   "source": [
    "filtered_assets = get_assets_in_region('Europe', ids)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>id</th>\n",
       "      <th>bbid</th>\n",
       "      <th>region</th>\n",
       "      <th>currency</th>\n",
       "      <th>exchange</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>MA4B66MW5E27U8P32SR</td>\n",
       "      <td>SMI</td>\n",
       "      <td>Europe</td>\n",
       "      <td>CHF</td>\n",
       "      <td>EBS</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>MA4B66MW5E27U8P32SY</td>\n",
       "      <td>SX5E</td>\n",
       "      <td>Europe</td>\n",
       "      <td>EUR</td>\n",
       "      <td>DTBX</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>MA4B66MW5E27U8NPZQY</td>\n",
       "      <td>DAX</td>\n",
       "      <td>Europe</td>\n",
       "      <td>EUR</td>\n",
       "      <td>XETR</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>MA4B66MW5E27U8P32GB</td>\n",
       "      <td>MXEA</td>\n",
       "      <td>Europe</td>\n",
       "      <td>USD</td>\n",
       "      <td>DTBX</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>MA4B66MW5E27U8NN95P</td>\n",
       "      <td>UKX</td>\n",
       "      <td>Europe</td>\n",
       "      <td>GBP</td>\n",
       "      <td>LNSE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>MA4B66MW5E27U8NN922</td>\n",
       "      <td>CAC</td>\n",
       "      <td>Europe</td>\n",
       "      <td>EUR</td>\n",
       "      <td>PARE</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>MA4B66MW5E27U8NN8LS</td>\n",
       "      <td>AEX</td>\n",
       "      <td>Europe</td>\n",
       "      <td>EUR</td>\n",
       "      <td>AMSE</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                    id  bbid  region currency exchange\n",
       "0  MA4B66MW5E27U8P32SR   SMI  Europe      CHF      EBS\n",
       "1  MA4B66MW5E27U8P32SY  SX5E  Europe      EUR     DTBX\n",
       "2  MA4B66MW5E27U8NPZQY   DAX  Europe      EUR     XETR\n",
       "3  MA4B66MW5E27U8P32GB  MXEA  Europe      USD     DTBX\n",
       "4  MA4B66MW5E27U8NN95P   UKX  Europe      GBP     LNSE\n",
       "5  MA4B66MW5E27U8NN922   CAC  Europe      EUR     PARE\n",
       "6  MA4B66MW5E27U8NN8LS   AEX  Europe      EUR     AMSE"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.DataFrame(filtered_assets, columns=['id', 'bbid', 'region', 'currency', 'exchange'])"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  },
  "pycharm": {
   "stem_cell": {
    "cell_type": "raw",
    "metadata": {
     "collapsed": false
    },
    "source": []
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
